<?php
/**
 * Created by PhpStorm.
 * User: qttx
 * Date: 2019/6/2
 * Time: 16:57
 */
namespace app\shop\model;

use think\Session;

class TaobaoModel extends BaseModel
{
  protected $tables="taobao";
   protected $table="xst_taobao";
    protected $rule = [
        'id' => '',
    ];
    protected $msg = [
        'id' => '',
    ];
    /*
     * 商品列表
     */
    public function getGoodsList()
    {
        $condition = 1;
        if(input('get.sub')){
            $keywords = input('keywords');
            if(!empty($keywords)){
                $condition .= " and xst_taobao.goods like '%".$keywords."%'";
            }
        }
        $goodsList = $this->field('*,xst_taobao.id as gid')
            ->where($condition)
            ->paginate(30,false,['query'=>request()->param()]);
        return $goodsList;
    }
    /*
     * 商品编辑
     */
    public function goodsEdit()
    {
        if(request()->post()){
            $datas=[
                'goods'=>input("goods"),
                'params'=>json_encode(input("params/a")),
                'small_img'=>input("small_img"),
                'content'=>input("content"),
                'stock'=>input("stock"),
                'sold'=>input("sold"),
                'old_price'=>input("old_price"),
                'price'=>input("price"),
                'sort'=>input("sort"),
                'img'=>implode(',',input("img/a")),
                'synopsis'=>implode(',',input("synopsis/a")),
                'shop'=>Session::get('shop'),
                'type'=>input("type"),
                'id'=>input("id"),
            ];
            $result = $this->allowField(true)->isUpdate(true)->save($datas);
            return $result;
        }else{
            $goodsInfo = $this->field('*,xst_taobao.id as gid')
                ->where('xst_taobao.id',input('id'))
                ->find();
//            dump( $goodsInfo);die;
            $goodsInfo['checkSynopsis'] = $goodsInfo['synopsis'];
            $goodsInfo['checkImg'] = $goodsInfo['img'];
            if( $goodsInfo['synopsis']!=''){
                $goodsInfo['synopsis'] = explode(',',$goodsInfo['synopsis']);
            }
            if( $goodsInfo['checkImg']!=''){
                $goodsInfo['img'] = explode(',',$goodsInfo['img']);
            }
        }
        return $goodsInfo;

    }
    /*
     * 商品添加
     */
    public function goodsAdd()
    {
        if (request()->post()) {
            $data = input("post.");
//           echo 33;die;
            if($data['img']==1 || $data['synopsis'] ==1){
                return ['code'=>0,'msg'=>'图片不能为空'];
            }
            $datas=[
                'goods'=>input("goods"),
                'params'=>json_encode(input("params/a")),
                'small_img'=>input("small_img"),
                'content'=>input("content"),
                'stock'=>input("stock"),
                'sold'=>input("sold"),
                'old_price'=>input("old_price"),
                'price'=>input("price"),
                'sort'=>input("sort"),
                'img'=>implode(',',$data['img']),
                'synopsis'=>implode(',',$data['synopsis']),
                'shop'=>Session::get('shop'),
                'type'=>input("type"),
            ];
            $query = $this->insert($datas);
            return $query;
        }else{
            $goodsInfo = $this->getEModel($this->tables);
            $goodsInfo['checkSynopsis'] = 0;
            $goodsInfo['checkImg'] = 0;
        }
        return $goodsInfo;

    }
    /*
     * 商品删除
     * */
    public function goodsDel()
    {
        $id = input("id");
        if(!empty($id)){
            $result = $this->where('id','in',$id)->delete();
            if($result){
                $status = ['code'=>1,'msg'=>'删除成功'];
            }else{
                $status = ['code'=>0,'msg'=>'删除失败'];
            }
        }else{
            $status = ['code'=>0,'msg'=>'请选择要删除的项'];
        }
        return $status;
    }
    /*
     * 商品简介
     * */
    public function lookSynopsis()
    {
        $id = input('id');
        $info = $this->getInfo(['id'=>$id],'synopsis');
        $info = explode(',',$info["synopsis"]);
        return $info;
    }
    /*
     * 商品简介
     * */
    public function lookImg()
    {
        $id = input('id');
        $info = $this->getInfo(['id'=>$id],'img');
        $info = explode(',',$info["img"]);
        return $info;
    }

    // 图片上传和保存数据
    public function upload($files, $i)
    {
        $fileName = md5(time()) . $i;
        $fileRoute = ROOT_PATH . '/upload/goods/';
        // 创建文件夹
        if (!file_exists($fileRoute)) {
            mkdir($fileRoute);
        }
        $info = $files->move($fileRoute, $fileName);
        if ($info) {
            return  '/upload/goods/'.$info->getFilename();
        } else {
            // 上传失败获取错误信息
            return $info->getError();
        }
    }
    //编辑商品状态
    public function updateStatus()
    {
        $data = input('post.');
        $id = input('id');
        $result = $this->where('id',$id)->update($data);
        if($result){
            $status = ['code'=>1,'msg'=>'修改成功'];
        }else{
            $status = ['code'=>1,'msg'=>'修改失败'];
        }
        return $status;
    }
    //上传图片
    public function uploadImg()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        $savePath = ROOT_PATH.'/upload/goods/';
        $newname = time().rand(0,99999);
        $info = $file->move($savePath,$newname);
        if($info){
            $filename = '/upload/goods/'.$info->getFilename();
            exit(json_encode(['code' => 1, 'msg' => '上传成功', 'filename' => $filename]));
        }else{
            exit(json_encode(['code' => 0, 'msg' => '上传失败']));
        }
    }
    //删除图片
    public function delImgs()
    {
        $data = input('post.');
        $img = $this->where('id',$data['id'])->find();
        $imgs = explode(',',$img['synopsis']);
        for($i=0;$i<count($imgs);$i++){
            if($imgs[$i] == $data['src']){
                unset($imgs[$i]);
            }
        }
        $info = implode(',',$imgs);
        $result = $this->where(['id'=>$data['id']])->update(['synopsis'=>$info]);
        if($result != false){
            $status = ['code'=>1,'msg'=>'删除成功'];
        }else{
            $status = ['code'=>0,'msg'=>'删除失败'];
        }
        return $status;
    }
    //删除图片
    public function delImg()
    {
        $data = input('post.');
        $img = $this->where('id',$data['id'])->find();
        $imgs = explode(',',$img['img']);
        for($i=0;$i<count($imgs);$i++){
            if($imgs[$i] == $data['src']){
                unset($imgs[$i]);
            }
        }
        $info = implode(',',$imgs);
        $result = $this->where(['id'=>$data['id']])->update(['img'=>$info]);
        if($result != false){
            $status = ['code'=>1,'msg'=>'删除成功'];
        }else{
            $status = ['code'=>0,'msg'=>'删除失败'];
        }
        return $status;
    }
}